<%@ Page Language="C#" ValidateRequest="false"%>
<script runat="server">
    private const string NoPic = "/Images/no_pic.gif";
    
    private void Page_Load(object sender, System.EventArgs e)
    {
        Response.ClearHeaders();
        Response.ClearContent();
        Response.Clear();
        Response.ContentType = "text/xml";
        Response.ContentEncoding = System.Text.UTF8Encoding.UTF8;
        Response.Charset = "utf-8";
    }
    
    protected string GetNickName(string memberno)
    {
        if (memberno.Length < 10) memberno = "T" + memberno.PadLeft(9, '0');
        string tmp = "";
        using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["www_user.user"].ConnectionString))
        {
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("select NickName from t_user where memberno='" + memberno + "'", conn);
            conn.Open();
            System.Data.SqlClient.SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                tmp = dr[0].ToString();
                break;
            }
            dr.Close();
            conn.Close();
            if(tmp=="") tmp = "select NickName from t_user where memberno='" + memberno + "'";
        }
        return tmp;
    }

    protected string GetAvatar(string memberno)
    {
        if (memberno.Length < 10) memberno = "T" + memberno.PadLeft(9, '0');
    	string Avatar = NoPic;
        using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["www_user.user"].ConnectionString))
        {
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("select Avatar from t_user where memberno='" + memberno + "'", conn);
            conn.Open();
            System.Data.SqlClient.SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
            	Avatar = dr[0].ToString();
                Avatar = (Avatar.Trim() == "" ? NoPic : Avatar);
                break;
            }
            dr.Close();
            conn.Close();
        }
        return Avatar;
    }
    
    protected void GetBuddList(string memberno)
    {
        if (memberno.Length < 10) memberno = "T" + memberno.PadLeft(9, '0');
        using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["www_user.user"].ConnectionString))
        {
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("select BuddyMemberNo,NickName,Avatar from T_user_LinkMan a, t_user b where IsAgree=1 and MyMemberNo='" + memberno + "' and a.BuddyMemberNo=b.MemberNo", conn);
            conn.Open();
            System.Data.SqlClient.SqlDataReader dr = cmd.ExecuteReader();
            string Avatar = "";
            while (dr.Read())
            {
                Avatar = dr[2].ToString();
                Avatar = (Avatar.Trim() == "" ? NoPic : Avatar);
                Response.Write("<user>");
                Response.Write(string.Format("<userid>{0}</userid>",dr[0].ToString().Trim('T').TrimStart('0')));
                Response.Write(string.Format("<displayname>{0}</displayname>",dr[1].ToString()));
                Response.Write("<images>");
                Response.Write(string.Format("<icon>http://www.italki.com{0}</icon>",Avatar));
                Response.Write(string.Format("<thumbnail>http://www.italki.com{0}</thumbnail>", Avatar));
                Response.Write("</images>");
                Response.Write("</user>");
            }
            dr.Close();
            conn.Close();
        }
    }

    protected void Getblocklist(string memberno)
    {
        if (memberno.Length < 10) memberno = "T" + memberno.PadLeft(9, '0');
        using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["www_user.user"].ConnectionString))
        {
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("select BuddyMemberNo from T_user_LinkMan where 1=2 and MyMemberNo='" + memberno + "'", conn);
            conn.Open();
            System.Data.SqlClient.SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                Response.Write(string.Format("<userid>{0}</userid>", dr[0].ToString().Trim('T').TrimStart('0')));
            }
            dr.Close();
            conn.Close();
        }
    }

    protected System.Data.DataTable GetUserInfo(string memberno)
    {
        using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["www_user.user"].ConnectionString))
        {
            if (memberno.Length < 9)
            {
                memberno = 'T' + memberno.PadLeft(9, '0');
            }
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("select datediff(yy,bod,getdate()) BOD,Gender,Nationality,MotherTongue,TargetLanguage from t_user_detail a,T_User_Language_Buddy_Set b where a.memberno='" + memberno + "' and a.memberno=b.memberno", conn);
            System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cmd);
            conn.Open();
            System.Data.DataTable dt = new System.Data.DataTable();
            da.Fill(dt);
            conn.Close();
            return dt;
        }
    }
</script>
<%
	// This is the primary method of communication between Userplane and your servers.  See the WMXML.rtf file
	// for detailed specs on how this file works and how to get it integrated with Userplane

	Response.Write( "<?xml version='1.0' encoding='utf-8'?>" );
	Response.Write( "<!-- COPYRIGHT Userplane 2006 (http://www.userplane.com) -->" );
	Response.Write( "<!-- WM version 1.8.13 -->" );
	Response.Write( "<icappserverxml>" );

	String strAction = Request.QueryString["action"];
	String strDomainID = Request.QueryString["domainID"];
	String strCallID = Request.QueryString["callID"];
	
	if( strAction != null && strDomainID != null )
	{
		if( strAction == "getDomainPreferences" )
		{
			// get the value from your database
			Response.Write( "<allowCalls>setBlockedStatus,sendConnectionList,startConversation</allowCalls>" );
			Response.Write( "<characterlimit>200</characterlimit>" );
			Response.Write( "<forbiddenwordslist>ass,bitch</forbiddenwordslist>" );
			Response.Write( "<smileys>" );
				Response.Write( "<smiley>" );
					Response.Write( "<name>Ultra Angry</name>" );
					Response.Write( "<image>http://images.italki.userplane.com/images/smiley/UltraAngry.jpg</image>" );
					Response.Write( "<codes>" );
						Response.Write( "<code><![CDATA[>>:O]]></code>" );
						Response.Write( "<code><![CDATA[>>:-O]]></code>" );
					Response.Write( "</codes>" );
				Response.Write( "</smiley>" );
				Response.Write( "<smiley>" );
					Response.Write( "<name>Angry</name>" );
					Response.Write( "<image>DELETE</image>" );
				Response.Write( "</smiley>" );
			Response.Write( "</smileys>" );
			Response.Write( "<maxvideobandwidth>20000</maxvideobandwidth>" );
			Response.Write( "<domainlogolarge>http://images.clearplane.userplane.com/im/images/UserplaneLogo.jpg</domainlogolarge>" );
			Response.Write( "<line1></line1>" );
			Response.Write( "<line2></line2>" );
            Response.Write("<line3></line3>");
			Response.Write( "<line4></line4>" );
            Response.Write("<line5></line5>");
			Response.Write( "<avEnabled>true</avEnabled>" );
			Response.Write( "<clickableUserName>true</clickableUserName>" );
            Response.Write("<clickableTextUserName>true</clickableTextUserName>");
			Response.Write( "<gameButton>true</gameButton>" );
			Response.Write( "<buddyListButton>true</buddyListButton>" );
			Response.Write( "<preferencesButton>true</preferencesButton>" );
			Response.Write( "<smileyButton>true</smileyButton>" );
			Response.Write( "<blockButton>true</blockButton>" );
			Response.Write( "<addBuddyEnabled>true</addBuddyEnabled>" );
			Response.Write( "<connectionTimeout>60</connectionTimeout>" );
			Response.Write( "<sendConnectionListInterval>0</sendConnectionListInterval>" );
			Response.Write( "<sendArchive>false</sendArchive>" );
			Response.Write( "<sendTextToImages>false</sendTextToImages>" );
			Response.Write( "<buttonBarColor></buttonBarColor>" );
			Response.Write( "<hideDropShadows>false</hideDropShadows>" );
			Response.Write( "<hideHelp>ture</hideHelp>" );
			Response.Write( "<showLocalUserIcon>false</showLocalUserIcon>" );
			Response.Write( "<conferenceCallEnabled>-1</conferenceCallEnabled>" );
			Response.Write( "<maxxmlretries>5</maxxmlretries>" );
			Response.Write( "<buttonIcons>" );
				Response.Write( "<action></action>" );
				Response.Write( "<add></add>" );
				Response.Write( "<block></block>" );
				Response.Write( "<bold></bold>" );
				Response.Write( "<buddyList></buddyList>" );
				Response.Write( "<italic></italic>" );
				Response.Write( "<preferences></preferences>" );
				Response.Write( "<print></print>" );
				Response.Write( "<smiley></smiley>" );
				Response.Write( "<soundOn></soundOn>" );
				Response.Write( "<soundOff></soundOff>" );
				Response.Write( "<underline></underline>" );
			Response.Write( "</buttonIcons>" );
			Response.Write( "<systemMessages>" );
				Response.Write( "<waiting>true</waiting>" );
				Response.Write( "<open>true</open>" );
				Response.Write( "<closed>true</closed>" );
				Response.Write( "<blocked>true</blocked>" );
				Response.Write( "<away>true</away>" );
				Response.Write( "<nonDeliveryMessage timeout='30' sendOnClose='true' sendOnTimeout='false' promptUser='false'>If [[DISPLAYNAME]] doesn't receive this message, they will be emailed when you close this window</nonDeliveryMessage>" );
				Response.Write( "<nonDeliveryConfirm></nonDeliveryConfirm>" );
				Response.Write( "<conferenceCallInvitation>Join me in a private anonymous phone call: [[NUMBER]]</conferenceCallInvitation>" );
				Response.Write( "<conferenceCallReminder>Join a private anonymous phone call: [[NUMBER]]</conferenceCallReminder>" );
				Response.Write( "<conferenceCallRetrievingNumber>Creating a private anonymous phone number...</conferenceCallRetrievingNumber>" );
			Response.Write( "</systemMessages>" );
			Response.Write( "<quickMessageList>" );
				Response.Write( "<message>" );
					Response.Write( "<title>Standard greeting</title>" );
					Response.Write( "<body>Welcome! How can I help you today?</body>" );
				Response.Write( "</message>" );
			Response.Write( "</quickMessageList>" );
		}
		else if( strAction == "getMemberID" )
		{
			String strSessionGUID = Request.QueryString["sessionGUID"];
			String strKey = Request.QueryString["key"];
			
			if( strSessionGUID != null )
			{
				// get the value from your database
                Response.Write("<memberid>" + strSessionGUID.Trim('T').TrimStart('0') + "</memberid>");
			}
		}
		else if( strAction == "startIC" )
		{
			String strUserID = Request.QueryString["memberID"];
			String strTargetUserID = Request.QueryString["targetMemberID"];
			
			if( strUserID != null && strTargetUserID != null )
			{
				// now that the target users window is open, we can remove the request from the db
				// the values are reversed because this call happens from the other direction
					
				// execute strQuery against your database
				
				Response.Write( "<member>" );
          Response.Write(String.Format("<displayname>{0}</displayname>", GetNickName(strUserID)));
					Response.Write( String.Format("<imagepath>http://www.italki.com{0}</imagepath>",GetAvatar(strUserID)));
					Response.Write( "<avEnabled>true</avEnabled>" );
					Response.Write( "<kissSmackEnabled>true</kissSmackEnabled>" );					
					Response.Write( "<showerrors>true</showerrors>" );
					Response.Write( "<sound>true</sound>" );
					Response.Write( "<focus>true</focus>" );
					Response.Write( "<autoOpenAV>true</autoOpenAV>" );
          Response.Write("<autoStartAudio>true</autoStartAudio>");
          Response.Write("<autoStartVideo>false</autoStartVideo>");
					Response.Write( "<backgroundColor></backgroundColor>" );
					Response.Write( "<fontColor></fontColor>" );
					Response.Write( "<quickMessageList ignoreNoTextEntry='false'>" );
						Response.Write( "<message>" );
							Response.Write( "<title>Standard Greeting</title>" );
							Response.Write( "<body>Happy to be here!</body>" );
						Response.Write( "</message>" );
					Response.Write( "</quickMessageList>" );
					Response.Write( "<noTextEntry>false</noTextEntry>" );
					Response.Write( "<sessionTimeout>-1</sessionTimeout>" );
					Response.Write( "<sessionTimeoutMessage>Your session has timed out</sessionTimeoutMessage>" );
				Response.Write( "</member>" );
				Response.Write( "<targetMember>" );
          System.Data.DataTable dt = GetUserInfo(strTargetUserID);
          Response.Write(String.Format("<displayname>{0}</displayname>",GetNickName(strTargetUserID)));
          Response.Write(String.Format("<line1>{0},{1},From {2}</line1>",dt.Rows[0][0],(dt.Rows[0][1].ToString()=="0"?"Female":"Male"),dt.Rows[0][2]));
          Response.Write(String.Format("<line2>Speaks {0}</line2>", dt.Rows[0][3]));
          Response.Write(String.Format("<line3>Learning {0}</line3>", dt.Rows[0][4]));
          Response.Write(String.Format("<line4>{0}</line4>", ""));
          Response.Write(String.Format("<line5>{0}</line5>", ""));
					Response.Write(String.Format("<imagepath>http://www.italki.com{0}</imagepath>",GetAvatar(strTargetUserID)));
					Response.Write( "<avEnabled>false</avEnabled>" );
					Response.Write( "<blocked>false</blocked>" );
					Response.Write( "<backgroundColor></backgroundColor>" );
					Response.Write( "<fontColor></fontColor>" );
				Response.Write( "</targetMember>" );
			}
		}
		else if( strAction == "addFriend" )
		{
			String strUserID = Request.QueryString["memberID"];
			String strTargetUserID = Request.QueryString["targetMemberID"];
			
			if( strUserID != null && strTargetUserID != null )
			{
				// handle the request, no response required
			}
		}
		else if( strAction == "sendConnectionList" )
		{
			String strXmlData = Request.Form["xmlData"];
		
			if( strXmlData != null )
			{
				/*
				EXAMPLE:
			
				<?xml version='1.0' encoding='iso-8859-1'?>
					<connectionList>
					<server>flashcom.yourserver.userplane.com</server>
					<c><f type="m">21</f><t>1</t></c>
					<c><f type="m">1</f><t>8</t></c>
					<c><f type="s">a6d5fe44</f><t>1</t></c>
					<c><f type="m">1</f><t>21</t></c>
				</connectionList>
				*/
			
				// update your database and no need to return anything
			}
		}
		else if( strAction == "setBlockedStatus" )
		{
			String strUserID = Request.QueryString["memberID"];
			String strTargetUserID = Request.QueryString["targetMemberID"];
			String strTrueFalse = Request.QueryString["trueFalse"];
			
			if( strUserID != null && strTargetUserID != null && strTrueFalse != null )
			{
				// handle the request, no response required
			}
		}
		else if( strAction == "startConversation" )
		{
			String strUserID = Request.QueryString["memberID"];
			String strTargetUserID = Request.QueryString["targetMemberID"];
			
			if( strUserID != null && strTargetUserID != null )
			{
				// check to see if there is already a request to open a window in the db
				
				// execute strQuery against your database
				
				
				{
					// if not, insert a request to have a window opened up on the target user's machine
					
					// execute strQuery against your database
				}
			}
		}
		else if( strAction == "notifyConnectionClosed" )
		{
			String strUserID = Request.QueryString["memberID"];
			String strTargetUserID = Request.QueryString["targetMemberID"];
			
			if( strUserID != null && strTargetUserID != null )
			{
				// since the orginating user is closing their window, don't open a window on the target user anymore
					
				// execute strQuery against your database
				
				// in addition, you can also use the strXmlData variable to get any messages that were never delivered to the targetUser.
				String strXmlData = Request.Form["xmlData"];
			}
		}
		else if( strAction == "sendPendingMessages" )
		{
			String strUserID = Request.QueryString["memberID"];
			String strTargetUserID = Request.QueryString["targetMemberID"];
			
			if( strUserID != null && strTargetUserID != null )
			{
				// you can use the strXmlData variable to get any messages that were never delivered to the targetUser.
				String strXmlData = Request.Form["xmlData"];
			}
		}
		else if( strAction == "sendArchive" )
		{
			// you can use the strXmlData variable to get the archived messages
			String strXmlData = Request.Form["xmlData"];
		}
	}
	
	Response.Write( "</icappserverxml>" );
%>